package com.mingqi.enetconnected.primary.mytask.adapter;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.adapter.CacheCall;
import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.base.MyApplication;
import com.mingqi.enetconnected.util.DeviceUtils;
import com.mingqi.enetconnected.util.resource.ResourceUtils;
import com.mingqi.enetconnected.view.Dialog.AddTroubleDialog;
import com.ruffian.library.widget.RTextView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * @author li
 * @version 1.0
 * @describe
 * @WeChat 18571658038
 * @date 2018/9/6 星期四.
 */

public class SafetyTroubleAdapter extends BaseQuickAdapter<String,BaseViewHolder> {

    private Context mContext;

    public SafetyTroubleAdapter(Context context, List<String> data) {
        super(R.layout.item_safety_trouble,data);

        mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {

        helper.setText(R.id.tv_title,item);
        final ImageView ivExpend = helper.getView(R.id.iv_expend);

        switch (item) {
            case "新增隐患" :

                helper.setVisible(R.id.tv_add_trouble,true);

                final RecyclerView rvExpandNewAdd = helper.getView(R.id.rv_expand);
                List<String> expandList = new ArrayList<>();
                for (int i = 0; i < 2; i++) {

                    expandList.add("");
                }

                final NewAddTroubleAdapter newAddAdapter = new NewAddTroubleAdapter(expandList,mContext);
                rvExpandNewAdd.setAdapter(newAddAdapter);
                rvExpandNewAdd.setLayoutManager(new LinearLayoutManager(mContext));
                rvExpandNewAdd.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                        .colorResId(R.color.space)
                        .size(DeviceUtils.dp2px(mContext,10))
                        .build());
                rvExpandNewAdd.setVisibility(View.GONE);

                helper.getView(R.id.tv_title).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int visibility = rvExpandNewAdd.getVisibility();
                        if(visibility == View.VISIBLE) {

                            rvExpandNewAdd.setVisibility(View.GONE);
                            ivExpend.setImageResource(R.drawable.ic_expand_false);
                        } else {

                            rvExpandNewAdd.setVisibility(View.VISIBLE);
                            ivExpend.setImageResource(R.drawable.ic_expand_true);
                        }
                    }
                });

                helper.getView(R.id.tv_add_trouble).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        AddTroubleDialog dialog = new AddTroubleDialog(mContext);
                        dialog.show();
                    }
                });

                break;

            case "历史隐患" :

                helper.setVisible(R.id.tv_add_trouble,false);

                final RecyclerView rvExpandHistory = helper.getView(R.id.rv_expand);
                List<String> historyList = new ArrayList<>();
                for (int i = 0; i < 2; i++) {

                    historyList.add("");
                }

                final HistoryTroubleAdapter historyAdapter = new HistoryTroubleAdapter(historyList);
                rvExpandHistory.setAdapter(historyAdapter);
                rvExpandHistory.setLayoutManager(new LinearLayoutManager(mContext));
                rvExpandHistory.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext)
                        .colorResId(R.color.space)
                        .size(DeviceUtils.dp2px(mContext,10))
                        .build());
                rvExpandHistory.setVisibility(View.GONE);

                helper.getView(R.id.tv_title).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int visibility = rvExpandHistory.getVisibility();
                        if(visibility == View.VISIBLE) {

                            rvExpandHistory.setVisibility(View.GONE);
                            ivExpend.setImageResource(R.drawable.ic_expand_false);
                        } else {

                            rvExpandHistory.setVisibility(View.VISIBLE);
                            ivExpend.setImageResource(R.drawable.ic_expand_true);
                        }
                    }
                });

                historyAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                        int id = view.getId();
                        switch (id) {
                            case R.id.tv_trouble_remark :

                                TextView textView = new TextView(MyApplication.sContext);
                                textView.setText("备注备注备注备注备注备注备注备注备注备注" +
                                        "备注备注备注备注备注备注备注备注备注备注");
                                textView.setGravity(Gravity.START);
                                textView.setTextColor(ResourceUtils.getColor(R.color.white));
                                textView.setMaxWidth(DeviceUtils.getWidth2(mContext) / 2);
                                int[] location = new int[2];
                                view.getLocationOnScreen(location);

                                textView.setBackgroundResource(R.drawable.tv_bg_radius);
                                textView.setPadding(DeviceUtils.dp2px(mContext,5),DeviceUtils.dp2px(mContext,5),
                                        DeviceUtils.dp2px(mContext,5),DeviceUtils.dp2px(mContext,5));

                                PopupWindow popupWindow = new PopupWindow(view);
                                popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
                                popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
                                popupWindow.setBackgroundDrawable(new BitmapDrawable());
                                popupWindow.setOutsideTouchable(true);
                                popupWindow.setFocusable(true);
                                popupWindow.setContentView(textView);
                                popupWindow.showAtLocation(view, Gravity.NO_GRAVITY,location[0] + view.getWidth() ,location[1]-DeviceUtils.dp2px(mContext,15));

                                break;
                            default:
                                break;
                        }
                    }
                });

                break;
            default:

                break;
        }
    }
}
